<?php

require_once 'Items.php';
require_once 'Orders.php';
require_once 'Properties.php';
require_once 'ItemProperties.php';
class OrderDetail {

    private $ID;
    private $ItemID;
    private $Price;
    private $ItemPropertyID;
    private $OrderID;
    private $Quantity;
    
    // object
    private $Order;
    private $Item;
    private $ItemProperty;

    public function getID() {
        return $this->ID;
    }

    public function setID($ID) {
        $this->ID = $ID;
    }

    public function getItemID() {
        return $this->ItemID;
    }

    public function setItemID($ItemID) {
        $this->ItemID = $ItemID;
    }

    public function getPrice() {
        return $this->Price;
    }

    public function setPrice($Price) {
        $this->Price = $Price;
    }

    public function getItemPropertyID() {
        return $this->ItemPropertyID;
    }

    public function setItemPropertyID($ItemPropertyID) {
        $this->ItemPropertyID = $ItemPropertyID;
    }

    public function getOrderID() {
        return $this->OrderID;
    }

    public function setOrderID($OrderID) {
        $this->OrderID = $OrderID;
    }

    public function getQuantity() {
        return $this->Quantity;
    }

    public function setQuantity($Quantity) {
        $this->Quantity = $Quantity;
    }

    public function getOrder() {
        return $this->Order;
    }

    public function setOrder($Order) {
        $this->Order = $Order;
    }

    public function getItem() {
        return $this->Item;
    }

    public function setItem($Item) {
        $this->Item = $Item;
    }

    public function getItemProperty() {
        return $this->ItemProperty;
    }

    public function setItemProperty($ItemProperty) {
        $this->ItemProperty = $ItemProperty;
    }

    public function add($db = null) {
        if ($db != null) {
            $sql = "INSERT INTO `order_detail`
                                (
                                 `OD_I_ID`,
                                 `OD_Price`,
                                 `OD_IP_ID`,
                                 `OD_O_ID`,
                                 `OD_Quantity`)
                    VALUES (
                            :OD_I_ID,
                            :OD_Price,
                            :OD_IP_ID,
                            :OD_O_ID,
                            :OD_Quantity);";

            $parameters = array(
                ':OD_I_ID', ':OD_Price',
                ':OD_IP_ID', ':OD_O_ID',
                ':OD_Quantity'
            );
            $values = array(
                $this->getItemID(), $this->getPrice(),
                $this->getItemPropertyID(), $this->getOrderID(),
                $this->getQuantity()
            );
            $types = array(
                PDO::PARAM_INT, PDO::PARAM_INT,
                PDO::PARAM_INT, PDO::PARAM_INT,
                PDO::PARAM_INT
            );
            return $db->executeNonQuery($sql, $parameters, $values, $types, true);
        }
    }

    public function getListByOrderID($db = null) {
        if ($db != null) {
            $sql = "SELECT
                        `OD_ID`,
                        `OD_I_ID`,
                        `OD_Price`,
                        `OD_IP_ID`,
                        `OD_O_ID`,
                        `OD_Quantity`
                      FROM `order_detail`
                      WHERE OD_O_ID = :OD_O_ID";
            $parameters = array(':OD_O_ID');
            $values = array($this->getOrderID());
            $types = array(PDO::PARAM_INT);
            $result = $db->executeQuery($sql, $parameters, $values, $types, true);
            if ($result != null) {
                // mảng danh sách trả về
                $rows = array();

                // lấy mỗi dòng dữ liệu đưa vào mảng danh sách trả về
                while ($row = $result->fetch()) {

                    $orderDetail = new OrderDetail();
                    $orderDetail->setID($row->OD_ID);
                    $orderDetail->setItemID($row->OD_I_ID);
                    $orderDetail->setPrice($row->OD_Price);
                    $orderDetail->setItemPropertyID($row->OD_IP_ID);
                    $orderDetail->setOrderID($row->OD_O_ID);
                    $orderDetail->setQuantity($row->OD_Quantity);
                    
                    $item = new Items();
                    $item->setID($orderDetail->getItemID());
                    $item = $item->getByID($db);
                    $orderDetail->setItem($item);
                    
                    
                    if($orderDetail->getItemPropertyID() !=''){
                        $itemProperty = new ItemProperties();
                        $itemProperty->setID($orderDetail->getItemPropertyID());
                        $itemProperty = $itemProperty->getItemPropertyByID($db);
                        $orderDetail->setItemProperty($itemProperty);
                    }
                    
                    
                    
                    
                    
                    $rows[] = $orderDetail;
                }
                return $rows;
            } else {
                return null;
            }
        }
    }

}

?>
